#include "Int_VL53L1X.h"     

#define dev 0x52
void Int_VL53L1X_Init(void)
{
    uint8_t byteData;
    uint16_t wordData;
    uint8_t sensorState = 0;
    // 进行重启VL53L1X
    HAL_GPIO_WritePin(VX_XSHUT_GPIO_Port, VX_XSHUT_Pin, GPIO_PIN_RESET);
    vTaskDelay(2);
    HAL_GPIO_WritePin(VX_XSHUT_GPIO_Port, VX_XSHUT_Pin, GPIO_PIN_SET);
    vTaskDelay(2);
    // 初始化VL53L1X
    VL53L1_RdByte(dev, 0x010F, &byteData);
    printf("VL53L1X Model_ID: %X\n", byteData);
    VL53L1_RdByte(dev, 0x0110, &byteData);
    printf("VL53L1X Module_Type: %X\n", byteData);
    VL53L1_RdWord(dev, 0x010F, &wordData);
    printf("VL53L1X: %X\n", wordData);
    while (sensorState == 0)
    {
        VL53L1X_BootState(dev, &sensorState);
        vTaskDelay(2);
    }
    VL53L1X_SensorInit(dev);

    VL53L1X_SetDistanceMode(dev, 2);           /* 1=short, 2=long */
    VL53L1X_SetTimingBudgetInMs(dev, 20);     /* in ms possible values [20, 50, 100, 200, 500] */
    VL53L1X_SetInterMeasurementInMs(dev, 20); /* in ms, IM must be > = TB */
    printf("VL53L1X Ultra Lite Driver Example running ...\n");
    VL53L1X_StartRanging(dev);
}
// 获取距离

uint16_t Int_VL53L1X_GetDistance(void)
{
    uint16_t Distance;
    uint8_t dataReady = 0;
    /* read and display data */
    while (dataReady == 0)
    {
        VL53L1X_CheckForDataReady(dev, &dataReady);
        vTaskDelay(2);
    }

    VL53L1X_GetDistance(dev, &Distance);
    /* This function must to be called to initialize the sensor with the default setting  */

    return Distance;
}
